#include "valueeditwnd.h"
#include <QtGui>
#include "syscontroller.h"
#include "touchspinbox.h"

//------------------------------------------------------------------------------
// PUBLIC INTERFACES
//------------------------------------------------------------------------------

ValueEditWnd::ValueEditWnd(int item_index, bool bias, QWidget *parent)
	: QDialog(parent),
	  sys_(SysController::instance()),
	  item_index_(item_index),
	  bias_(bias),
	  old_rng_(bias ? sys_->itemBiasRng(item_index) : sys_->itemMeasRng(item_index))  // NOTE!: BC
{
	InitWidgets();

	rangeCombo->addItems(sys_->ItemRangeList(item_index, bias));
	QString range_str = bias ?  sys_->itemBiasRng(item_index) 
							 : sys_->itemMeasRng(item_index);
	rangeCombo->setCurrentIndex(rangeCombo->findText(range_str));
}
//ValueEditWnd::ValueEditWnd(int value_type, QWidget *parent)
//	: QDialog(parent),
//	  sys_(SysController::instance())
//	  item_index_(item_index),
//	  bias_(bias)
//{
//	InitWidgets();
//}

ValueEditWnd::~ValueEditWnd()
{
	delete rangeCombo;
    delete spinLower;
    delete spinUpper;
    delete dspinLower;
    delete dspinUpper;
    delete headLayout;
    delete spinLayout;
    delete vLayout;
    delete okButton;
    delete cancelButton;
}

//------------------------------------------------------------------------------
// SLOTS
//------------------------------------------------------------------------------

void ValueEditWnd::onRangeChanged(const QString &rng_str)
{
	if (bias_) {
		sys_->setItemBiasRng(item_index_, rng_str);
	} else {
		sys_->setItemMeasRng(item_index_, rng_str);
	}
	
	RefreshSpinBox();
}


void ValueEditWnd::dialogShouldReturn()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	if (button == cancelButton) {
		if (bias_) {
			sys_->setItemBiasRng(item_index_, old_rng_);
		} else {
			sys_->setItemMeasRng(item_index_, old_rng_);
		}
		reject();
	} else {
		if (bias_) {
			sys_->setItemBiasVal(item_index_,
					QString::number(sys_->IsItemRangeInteger(item_index_, bias_) 
					? spinLower->value() : dspinLower->value()));
		} else {
			double lower_val = sys_->IsItemRangeInteger(item_index_, bias_) 
							 ? spinLower->value() : dspinLower->value();
			double upper_val = sys_->IsItemRangeInteger(item_index_, bias_) 
							 ? spinUpper->value() : dspinUpper->value();
			if (lower_val > upper_val) {
				// MSGBOX TO TELL USER
				return;
			}
			sys_->setItemMeasVal(item_index_, true, QString::number(lower_val));
			sys_->setItemMeasVal(item_index_, false, QString::number(upper_val));
		}
		accept();
	}
}

//------------------------------------------------------------------------------
// PRIVATE FUNCTIONS
//------------------------------------------------------------------------------

void ValueEditWnd::InitWidgets()
{
	rangeCombo	= new QComboBox;
	spinLower	= new TouchSpinBox;
	spinUpper	= new TouchSpinBox;
	dspinLower	= new TouchDoubleSpinBox;
	dspinUpper	= new TouchDoubleSpinBox;
	headLayout	= new QHBoxLayout;
	spinLayout	= new QHBoxLayout;
	vLayout		= new QVBoxLayout;
	okButton	= new QPushButton(tr("OK"));
	cancelButton= new QPushButton(tr("Cancel"));

	headLayout->addWidget(rangeCombo);
	headLayout->addWidget(cancelButton);
	headLayout->addWidget(okButton);
	spinLayout->addWidget(spinLower);
	spinLayout->addWidget(spinUpper);
	spinLayout->addWidget(dspinLower);
	spinLayout->addWidget(dspinUpper);
	vLayout->addLayout(headLayout);
	vLayout->addLayout(spinLayout);

	setLayout(vLayout);
}

void ValueEditWnd::RefreshSpinBox()
{
	if (bias_) {
		if (sys_->IsItemRangeInteger(item_index_, bias_)) {
			spinLower->setValue(sys_->itemBiasVal(item_index_).toDouble());
			spinUpper->setVisible(false);
			dspinLower->setVisible(false);
			dspinUpper->setVisible(false);
		} else  {
			dspinLower->setValue(sys_->itemBiasVal(item_index_).toDouble());
			dspinUpper->setVisible(false);
			spinLower->setVisible(false);
			spinUpper->setVisible(false);
		}
	} else {
		if (sys_->IsItemRangeInteger(item_index_, bias_)) {
			spinLower->setValue(sys_->itemMeasVal(item_index_, true).toDouble());
			spinUpper->setValue(sys_->itemMeasVal(item_index_, false).toDouble());
			dspinLower->setVisible(false);
			dspinUpper->setVisible(false);
		} else  {
			dspinLower->setValue(sys_->itemMeasVal(item_index_, true).toDouble());
			dspinUpper->setValue(sys_->itemMeasVal(item_index_, false).toDouble());
			spinLower->setVisible(false);
			spinUpper->setVisible(false);
		}
	}
}
